﻿using System;
using UnityEngine;

namespace Jinndev {

    public abstract class ChangeableData {

        public string OnChangeEventKey { get; protected set; }

        public ChangeableData(string onChangeEventKey) {
            OnChangeEventKey = onChangeEventKey;
        }

        public void InvokeOnChange(params object[] args) {
            if (!string.IsNullOrWhiteSpace(OnChangeEventKey)) {
                EventManager.Invoke(OnChangeEventKey, args);
            }
        }

        public void AutoSetOnChange(MonoBehaviour behaviour, Action<EventData> action) {
            if (!string.IsNullOrWhiteSpace(OnChangeEventKey)) {
                EventManager.AutoSet(behaviour, OnChangeEventKey, action);
            }
        }

        public void AddOnChange(Action<EventData> action) {
            if (!string.IsNullOrWhiteSpace(OnChangeEventKey)) {
                EventManager.Add(OnChangeEventKey, action);
            }
        }

        public void RemoveOnChange(Action<EventData> action) {
            if (!string.IsNullOrWhiteSpace(OnChangeEventKey)) {
                EventManager.Remove(OnChangeEventKey, action);
            }
        }

        /// <summary>
        /// 自动设置OnChange事件回调，当目标behaviour销毁时会自动移除事件回调
        /// </summary>
        /// <param name="behaviour"></param>
        /// <param name="action"></param>
        /// <param name="inventoryArray"></param>
        public static void AutoSetOnChange(MonoBehaviour behaviour, Action<EventData> action, params ChangeableData[] array) {
            if (array == null) {
                return;
            }
            foreach (ChangeableData data in array) {
                data.AutoSetOnChange(behaviour, action);
            }
        }

    }


}
